package com.hu.common.util.user;

import com.alibaba.fastjson.JSON;


import com.hu.common.config.Config;
import com.hu.common.exception.RequestBodyFormatException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 页面请求帮助类
 *
 * @Author 63198
 */
public class RequestUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtil.class);

    public static HttpServletRequest getServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpServletResponse getServletResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();

    }

    public static String getBody() throws RequestBodyFormatException {
        return getBody(getServletRequest());
    }

    public static String getBody(HttpServletRequest request) throws RequestBodyFormatException {

        if (request.getAttribute("body") != null) {
            return request.getAttribute("body").toString();
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            request.setAttribute("body", sb.toString());
            return sb.toString();
        } catch (Exception ex) {
            throw new RequestBodyFormatException("读取包体失败");
        }
    }

    public static <T> T getBody(HttpServletRequest request, Class<T> clazz) throws RequestBodyFormatException {
        String bodyStr = getBody(request);
        T ob = JSON.parseObject(bodyStr, clazz);
        if (!StringUtils.isEmpty(bodyStr.trim()) && ob == null) {
            throw new RequestBodyFormatException("包体格式不正确");
        }
        return ob;
    }

    public static <T> T getBody(Class<T> clazz) throws RequestBodyFormatException {
        return getBody(getServletRequest(), clazz);
    }

    public static int getPageNum() {
        ServletRequest request = getServletRequest();
        String pageParamName = "page";
        String currentParamName = "current";
        if (request.getParameter(pageParamName) != null) {
            return Integer.parseInt(request.getParameter(pageParamName));
        }
        if (request.getParameter(currentParamName) != null) {
            return Integer.parseInt(request.getParameter(currentParamName));
        }

        return 1;
    }


    public static int getPageSize() {
        ServletRequest request = getServletRequest();
        String pageSizeParamName = "pageSize";
        if (request.getParameter(pageSizeParamName) != null) {
            int pageSize = Integer.parseInt(request.getParameter("pageSize"));
            if (pageSize > Config.maxPageSize) {
                pageSize = Config.maxPageSize;
            }
            return pageSize;
        } else {
            return 10;
        }
    }
}
